<template>
    <div class="con_store_main">
        <!--新增窗口-->
        <el-dialog title="新增管理员" :visible.sync="addBox" width="50%" :close-on-click-modal="true" center>
            <div>
                <el-form :model="addForm" :rules="rules">
                    <el-form-item label="名称" prop="name">
                        <el-input v-model="addForm.name" placeholder="输入管理员名称"></el-input>
                    </el-form-item>
                    <el-form-item label="账号" prop="account">
                        <el-input v-model="addForm.account" placeholder="输入管理员账号"></el-input>
                    </el-form-item>
                    <el-form-item label="密码" prop="password">
                        <el-input v-model="addForm.password" placeholder="输入管理员密码"></el-input>
                    </el-form-item>
                    <div>
                        <el-button @click="confirmAdd">確認添加</el-button>
                    </div>
                </el-form>
            </div>
        </el-dialog>
        <el-form :inline="true" :model="formInline" class="demo-form-inline">
            <el-form-item label="管理员名称">
                <el-input v-model="formInline.name" placeholder="管理员名称"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button @click="findData" type="primary" icon="el-icon-search">搜索</el-button>
            </el-form-item>
            <el-form-item>
                <el-button @click="addAdmin" type="primary" icon="el-icon-search">新增</el-button>
            </el-form-item>
        </el-form>
        <el-table class="table" :data="tableData" style="width: 100%" v-loading="loading"
            :default-sort="{ prop: 'index', order: 'descending' }">
            <el-table-column type="index" label="序号" sortable width="100">
            </el-table-column>
            <el-table-column label="头像" width="120">
                <template slot-scope="scope">
                    <el-avatar :src="scope.row.avatar"></el-avatar>
                </template>
            </el-table-column>
            <el-table-column prop="name" label="名称" width="100">
            </el-table-column>
            <el-table-column prop="account" label="账号" width="150">
            </el-table-column>
            <el-table-column prop="createTime" label="创建时间">
            </el-table-column>
            <el-table-column prop="createAdminId" label="创建人">
            </el-table-column>
            <el-table-column label="操作">
                <template slot-scope="scope">
                    <el-button type="text" size="small" @click="deleteAdmin(scope.row)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination style="margin: auto; margin-top: 8px" :hide-on-single-page="value"
            :current-page.sync="currentPage" :page-size="6" :pager-count="11" layout="prev, pager, next"
            :total="this.total" @current-change="handleSizeChange">
        </el-pagination>
    </div>
</template>
<script>
import { request } from "@/network/request/request";
export default {
    mounted() {
        this.findData();
    },
    data() {
        return {
            rules: {
                name: [
                    { required: true, message: '请输入管理员名称', trigger: 'blur' },
                    { min: 1, max: 10, message: '长度在 1 到 10 个字符', trigger: 'blur' }
                ],
                account: [
                    { required: true, message: '请输入管理员账号', trigger: 'blur' },
                    { min: 4, max: 12, message: '长度在 4 到 12 个字符', trigger: 'blur' }
                ],
                password: [
                    { required: true, message: '请输入管理员密码', trigger: 'blur' },
                    { min: 6, max: 16, message: '长度在 6 到 16 个字符', trigger: 'blur' }
                ],
            },
            addForm: {
                name: '',
                account: '',
                password: '',
            },
            accountForm: {
                account: localStorage.getItem("account"),
                password: localStorage.getItem("password"),
            },
            formInline: { name: '' },
            updateBox: false,
            addBox: false,
            currentPage: 1, // 当前页码
            pageSize: 6, // 每页显示的记录数
            total: 0, // 总数目，默认设置为 0
            loading: false,
            value: false,
            tableData: [],
            adminAccount: null,
        };
    },
    methods: {
        time() {
            this.loading = true;
            setTimeout(() => {
                this.loading = false;
            }, 600);
        },
        handleSizeChange() {
            this.findData();
        },
        deleteAlert(row) {
            this.$confirm('此操作将永久删除该管理员, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                this.deleteRequest(row);  // 确保这个方法存在，并且实现了删除逻辑
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });
            });
        },

        addAdmin() {
            this.addBox = true;
        },
        deleteAdmin(row) {
            this.deleteAlert(row);
        },

        async findData() {
            this.time();
            const params = {
                name: this.formInline.name,
                currentPage: this.currentPage /* 当前页码 */,
                pageSize: this.pageSize /* 页码大小 */,
                account:localStorage.getItem("account")
            };
            const config = {
                url: "api/admin",
                method: "get",
                params: params,
            };
            const res = await request(config);
            if (res.data.code === 1) {
                this.total = res.data.data.total; /* 得到数据的总数 */
                this.tableData = res.data.data.pageList; /* 得到数据 */
            }
        },
        async deleteRequest(row) {
            let id = row.adminId;
            const config = {
                url: `api/admin/${id}`,/* 通过路径参数 路径参数 */
                method: "delete",
            }
            const res = await request(config);
            if (res.data.code === 1) {
                this.$message({
                    type: 'success',
                    message: '删除成功'
                });
                this.findData();
            } else {
                this.$message({
                    type: 'error',
                    message: '删除失败'
                });
            }

        },
        
        async confirmAdd() {
           
            const config = {
                url: "api/admin/addAdmin",
                method: "post",
                data: {
                    name: this.addForm.name,
                    account: this.addForm.account,
                    password: this.addForm.password,
                    adminAccount:localStorage.getItem("account"),
                }
            }
            const res = await request(config);
            if (res.data.code == 1) {
                this.$message.success("添加成功");
                this.addForm.name = '';
                this.addForm.account = '';
                this.addForm.password = '';
                this.addBox = false;
            } else {
                this.$message.error(res.data.msg);
            }
            this.findData();
        },
    },
};
</script>
<style scoped>
.con_store_main {
    height: 498px;
    display: flex;
    flex-direction: column;
    align-content: center;
    justify-content: center;
}

.ic-status {
    display: flex;
    align-content: center;
}
</style>